package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.Student;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.enums.UserTypeEnum;
import com.jyy.prefabricated.service.QuestionBankService;
import com.jyy.prefabricated.service.StudentQuestionBankService;
import com.jyy.prefabricated.utils.UserUtils;
import com.jyy.prefabricated.vo.QuestionBankVo;
import com.jyy.prefabricated.vo.StudentQuestionBankNoteVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 学员题库
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class StudentQuestionBankController {

    @Autowired
    private UserUtils userUtils;

    @Autowired
    private StudentQuestionBankService studentQuestionBankService;

    @Autowired
    private QuestionBankService questionBankService;

    /**
     * 学员查询题库
     * @param name
     * @param categoryId
     * @param pageable
     * @return
     */
    @GetMapping("/student-question-bank")
    public AjaxResult selectByParams(@RequestParam(required = false,defaultValue = "") String name, @RequestParam(required = false) Integer categoryId, Pageable pageable){
        log.info("Rest request to find student-question-bank by name:{},categoryId:{}",name,categoryId);
        SysUser user = userUtils.getCurrentUser();
        List<QuestionBankVo> questionBanks = new ArrayList<>();
        if(UserTypeEnum.STUDENT.getValue().equals(user.getUserType())){
            List<Integer> categoryIds = questionBankService.selectDeepCategoryIds(categoryId);
            PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
            questionBanks = studentQuestionBankService.selectQuestionBankByStudent(name,user.getStudent().getId(),categoryIds);
        }
        PageInfo<QuestionBankVo> result = new PageInfo<>(questionBanks);
        return AjaxResult.success(result);
    }

    /**
     * 学员查询错题本
     * @param name
     * @param categoryId
     * @param pageable
     * @return
     */
    @GetMapping("/student-question-bank-error")
    public AjaxResult selectErrorByParams(@RequestParam(required = false,defaultValue = "") String name, @RequestParam(required = false) Integer categoryId, Pageable pageable){
        log.info("Rest request to find student-error-question-bank by name:{},categoryId:{}",name,categoryId);
        SysUser user = userUtils.getCurrentUser();
        List<QuestionBankVo> questionBanks = new ArrayList<>();
        if(UserTypeEnum.STUDENT.getValue().equals(user.getUserType())){
            List<Integer> categoryIds = questionBankService.selectDeepCategoryIds(categoryId);
            PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
            questionBanks = studentQuestionBankService.selectErrorQuestionBankByStudent(name,user.getStudent().getId(),categoryIds);
        }

        PageInfo<QuestionBankVo> result = new PageInfo<>(questionBanks);
        return AjaxResult.success(result);
    }

    /**
     * 学员查询错题本详情
     * @param questionBankId
     * @return
     */
    @GetMapping("/student-question-bank-error-detail/{questionBankId}")
    public AjaxResult selectErrorByParams(@PathVariable Integer questionBankId){
        Student student = userUtils.getCurrentStudent();
        log.info("Rest request to find student-note-question-bank-detail by questionBankId:{},studentId:{}",questionBankId,student.getId());
        StudentQuestionBankNoteVo result = studentQuestionBankService.fetchErrorQuestionsByQuestionBankId(questionBankId,student.getId());
        return AjaxResult.success(result);
    }

    /**
     * 学员删除错题本题库
     * @param questionBankId
     * @return
     */
    @DeleteMapping("/student-question-bank-error/{questionBankId}")
    public AjaxResult deleteErrorQuestionBank(@PathVariable Integer questionBankId){
        Student student = userUtils.getCurrentStudent();
        log.info("Rest request to delete student-error-question-bank by questionBankId:{},studentId:{}",questionBankId,student.getId());
        studentQuestionBankService.deleteErrorQuestionBank(questionBankId,student.getId());
        return AjaxResult.success();
    }

    /**
     * 学员查询收藏题库
     * @param name
     * @param categoryId
     * @param pageable
     * @return
     */
    @GetMapping("/student-question-bank-collection")
    public AjaxResult selectCollectionByParams(@RequestParam(required = false,defaultValue = "") String name, @RequestParam(required = false) Integer categoryId, Pageable pageable){
        log.info("Rest request to find student-collection-question-bank by name:{},categoryId:{}",name,categoryId);
        SysUser user = userUtils.getCurrentUser();
        List<QuestionBankVo> questionBanks = new ArrayList<>();
        if(UserTypeEnum.STUDENT.getValue().equals(user.getUserType())){
            List<Integer> categoryIds = questionBankService.selectDeepCategoryIds(categoryId);
            PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
            questionBanks = studentQuestionBankService.selectCollectionQuestionBankByStudent(name,user.getStudent().getId(),categoryIds);
        }


        PageInfo<QuestionBankVo> result = new PageInfo<>(questionBanks);
        return AjaxResult.success(result);
    }

    /**
     * 学员查询笔记题库
     * @param name
     * @param categoryId
     * @param pageable
     * @return
     */
    @GetMapping("/student-question-bank-note")
    public AjaxResult selectNoteByParams(@RequestParam(required = false,defaultValue = "") String name, @RequestParam(required = false) Integer categoryId, Pageable pageable){
        log.info("Rest request to find student-note-question-bank by name:{},categoryId:{}",name,categoryId);
        SysUser user = userUtils.getCurrentUser();
        List<QuestionBankVo> questionBanks = new ArrayList<>();
        if(UserTypeEnum.STUDENT.getValue().equals(user.getUserType())){
            List<Integer> categoryIds = questionBankService.selectDeepCategoryIds(categoryId);
            PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
            questionBanks = studentQuestionBankService.selectNoteQuestionBankByStudent(name,user.getStudent().getId(),categoryIds);
        }
        PageInfo<QuestionBankVo> result = new PageInfo<>(questionBanks);
        return AjaxResult.success(result);
    }

    /**
     * 学员查询笔记题库详情
     * @param questionBankId
     * @return
     */
    @GetMapping("/student-question-bank-note-detail/{questionBankId}")
    public AjaxResult selectNoteDetail(@PathVariable Integer questionBankId){
        Student student = userUtils.getCurrentStudent();
        log.info("Rest request to find student-note-question-bank-detail by questionBankId:{},studentId:{}",questionBankId,student.getId());
        StudentQuestionBankNoteVo result = studentQuestionBankService.fetchQuestionNotesByQuestionBankId(questionBankId,student.getId());
        return AjaxResult.success(result);
    }
}
